const crypto = require('crypto');
const Response = require('./Response')
const {
	getTime,
} = require('../utils/dateUtils')
const {
	_UltDB,
	_WebDB,
} = global

// const {
// 	ed2kersColPromise,
// } = require('../dbs/db-mongo')

module.exports = {

	/**
	 * 资源分类列表
	 * @param  {[type]} params [description]
	 * @return {[type]}        [description]
	 */
	menu: async function(params) {
		let ed2kersCol = await ed2kersColPromise
		// let typeList = await ed2kersCol.distinct('type')
		// let docs = []
		// for (let i = 0; i < typeList.length; i++) {
		// 	let type = typeList[i]
		// 	let list = await ed2kersCol.distinct('category', {
		// 		type,
		// 	})
		// 	docs.push({
		// 		type,
		// 		list,
		// 	})
		// }
		let docs = await ed2kersCol.distinct('category', {
			type: '图书',
		})
		return new Response('000', '成功', {
			docs,
		})
	},

	/**
	 * 资源列表
	 * @param  {[type]} params [description]
	 * @return {[type]}        [description]
	 */
	list: async function(params) {
		let {
			type,
			category,
			page = 1,
			num = 30,
		} = params
		let query = {
			type: '图书',
			category,
		}
		let ed2kersCol = await ed2kersColPromise
		let total = await ed2kersCol.count(query)
		let docs = await ed2kersCol.find(query)
			.skip((page - 1) * num).limit(num).toArray();
		return new Response('000', '成功', {
			total,
			docs,
		})
	},

}

/**
 * 密码加密
 * @param  {[type]} password [description]
 * @return {[type]}          [description]
 */
function encPwd(password) {
	return crypto.createHash('md5').update(password).digest('hex');
}